public class demo1 {

    public static void main(String[] args) {
        String s = "hello";
        //字符串转数组
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }//h e l l o
        System.out.println();
        //数组转字符串
        String s2 = new String(array);
        System.out.println(s2);//hello
    }

    public static void main7(String[] args) {
        String s = "hello";
        String s1 = s.toUpperCase();
        System.out.println(s1);//HELLO

        String s2 = "HelLO";
        System.out.println(s2.toLowerCase());//hello
    }

    public static void main6(String[] args) {
        //数字转字符串 需要用到 valueof 方法
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        //字符串转数字
        int data1 = Integer.parseInt("1234");
        double data2 = Double.parseDouble("12.34");
        System.out.println(data1+1);//证明字符串转为数字了
        System.out.println(data2);
    }

    public static void main5(String[] args) {
        String s = "ababcabcd";
        char ch = s.charAt(5);//获得指定位置的元素
        System.out.println(ch);
        //通过循环获得字符串的所有字符
        for (int i = 0; i < s.length(); i++) {
            char ch1 = s.charAt(i);
            System.out.print(ch1 + " ");
        }
        int index = s.indexOf("c");//默认是从0位置开始找
        System.out.println(index);//4
        int index2 = s.indexOf("c",2);
        System.out.println(index2);//4
        //倒着往回的第一个ab
        int index3 = s.lastIndexOf("ab");
        System.out.println(index3);//5
        int index4 = s.lastIndexOf("ab",4);
        System.out.println(index4);//2
    }

    public static void main4(String[] args) {
        String s1 = new String("abCd");
        String s2 = new String("abcd");

        System.out.println(s1.compareTo(s2));//-32
        //忽略大小写
        System.out.println(s1.compareToIgnoreCase(s2));//0
    }

    public static void main3(String[] args) {
        String s1 = new String("ab");
        String s2 = new String("abcd");
        //比较字符串的大小
        //1.根据对应字符比较
        //2.对应字符无法比较出来 谁大 谁小 此时 就看长度
        System.out.println(s1.compareTo(s2));//s1和s2进行比较
    }

    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = s1;

        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//true
        System.out.println(s2.equals(s3));//true
        //对于引用类型变量，== 比较两个引用变量引用的是否为同一个对象。
        System.out.println(s1 == s2);//false
        System.out.println(s1 == s3);//true
        System.out.println(s2 == s3);//false
    }

    public static void main1(String[] args) {
        //使用常量串构造
        String s = "hello";
        //直接newString对象
        String s2 = new String("hello");
        //使用字符数组进行构造
        char[] array = {'a','b','c','d'};
        String s3 = new String(array);

        System.out.println(s);
        System.out.println(s2);
        System.out.println(s3);
    }
}
